package com.market.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.FeedBack;
import com.market.domain.User;
import com.market.domain.transients.Email;
import com.market.manager.FeedBackManager;
import com.market.web.model.FeedBackModel;
import com.market.web.model.UserModel;

@Controller("feedBackController")
@Scope("session")
public class FeedBackController extends
		GenericController<FeedBackManager, FeedBackModel, FeedBack> {

	@Autowired
	private UserModel userModel;

	@Autowired
	private EmailController emailController;

	@Autowired
	@Override
	protected void setModel(FeedBackModel model) {
		this.model = model;

	}

	@Autowired
	@Override
	protected void setManager(FeedBackManager manager) {
		this.manager = manager;
	}

	public void preFeedBack() {
		FeedBack feedBack = this.model.getNewObject();
		User user = userModel.getAuthenticatedUser();
		if (user != null) {
			feedBack.setUserName(user.getLastName() + " " + user.getFirstName());
			feedBack.setEmail(user.getEmail());
			feedBack.setTelephone(user.getTelephone());
		}
		this.setModelCurrent(feedBack);
	}

	public String submitFeedBack() {
		super.saveCurrent();
		return "FEED BACK SUBMIT SUCCEED";
	}

	public void sendAnswer() {
		FeedBack feedBack = this.getModelCurrent();
		Email answer = feedBack.getAnswerEmail();
		emailController.sendEmail(answer);

	}

}
